package com.spsoft.vatoutinv.controller.matchflow;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spsoft.core.annotation.AnonymousAccess;
import com.spsoft.core.annotation.Log;
import com.spsoft.core.controller.BaseController;
import com.spsoft.core.enums.LogActionType;
import com.spsoft.core.enums.LogMenuEnum;
import com.spsoft.core.model.Result;
import com.spsoft.vatoutinv.enums.CommonEnum;
import com.spsoft.vatoutinv.model.dto.matchflow.*;
import com.spsoft.vatoutinv.model.vo.matchflow.VmsMatchFlowApplyVo;
import com.spsoft.vatoutinv.model.vo.matchflow.VmsMatchInvoiceInfoVo;
import com.spsoft.vatoutinv.model.vo.matchflow.VmsMatchInvoicePageVo;
import com.spsoft.vatoutinv.model.vo.matchflow.VmsMatchRecordVo;
import com.spsoft.vatoutinv.model.vo.vmstransactionflowinvoice.VmsTransactionFlowInvoicePageVo;
import com.spsoft.vatoutinv.service.matchflow.VmsMatchFlowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 手工匹配流水申请 前端控制器
 * </p>
 *
 * @author chenxudong
 * @since 2024-06-25
 */
@RestController
@Api(tags = {"手工匹配流水申请管理"})
@RequestMapping("/matchFlow")
@RequiredArgsConstructor
@Validated
@Slf4j
public class VmsMatchFlowController extends BaseController {

    public static final String LOG_MSG = "(手工匹配流水申请控制层)";

    private final VmsMatchFlowService vmsMatchFlowService;

    @ApiOperation("手工匹配流水主页面-分页查询")
    @AnonymousAccess(value = "/pageApply", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "手工匹配流水主页面-分页查询", type = LogActionType.SELECT, menuName = LogMenuEnum.VMS_MATCH_FLOW)
    public Result<IPage<VmsMatchFlowApplyVo>> pageApply(@RequestBody @Validated VmsMatchFlowPageDto pageDto) {
        pageDto.setApplyType(CommonEnum.MATCH_FLOW_TYPE_0.getKey());
        log.info("{}手工匹配流水主页面-分页查询入参：{}", LOG_MSG, JSONObject.toJSONString(pageDto));
        return vmsMatchFlowService.pageApply(pageDto);
    }

    @ApiOperation("手工匹配流水主页面-数据导出")
    @AnonymousAccess(value = "/export", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "手工匹配流水主页面-数据导出", type = LogActionType.DOWNLOAD, menuName = LogMenuEnum.VMS_MATCH_FLOW)
    public void exportApply(@RequestBody @Validated VmsMatchFlowPageDto pageDto, HttpServletResponse response) {
        pageDto.setApplyType(CommonEnum.MATCH_FLOW_TYPE_0.getKey());
        log.info("{}手工匹配流水主页面-数据导出入参：{}", LOG_MSG, JSONObject.toJSONString(pageDto));
        vmsMatchFlowService.export(pageDto, response);
    }

    @ApiOperation("发票信息查询页面-分页查询")
    @AnonymousAccess(value = "/pageInvoice", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "发票信息查询页面-分页查询", type = LogActionType.SELECT, menuName = LogMenuEnum.VMS_MATCH_FLOW)
    public Result<IPage<VmsMatchInvoicePageVo>> pageInvoice(@RequestBody VmsMatchInvoicePageDto pageDto) {
        pageDto.setDeptCode(getDeptCode());
        log.info("{}发票信息查询页面-分页查询，入参：{}", LOG_MSG, JSONObject.toJSONString(pageDto));
        return vmsMatchFlowService.pageInvoice(pageDto);
    }

    @ApiOperation("新增子界面-快速添加")
    @AnonymousAccess(value = "/fastAdd", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "新增子界面-快速添加", type = LogActionType.ADD, menuName = LogMenuEnum.VMS_MATCH_FLOW)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fpdm", value = "发票代码", dataTypeClass = String.class, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "fphm", value = "发票号码", dataTypeClass = String.class, dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "applyId", value = "审批流id", dataTypeClass = Long.class, dataType = "Long", paramType = "query", required = true)
    })
    public Result<VmsMatchInvoiceInfoVo> fastAdd(@RequestParam(value = "fpdm", required = false) String fpdm,
                                                 @RequestParam(value = "fphm") String fphm,
                                                 @RequestParam(value = "applyId") Long applyId) {
        log.info("{}新增子界面-快速添加，发票代码：{}，发票号码：{}，审批流id：{}", LOG_MSG, fpdm, fphm, applyId);
        return vmsMatchFlowService.fastAdd(fpdm, fphm, applyId, getUser());
    }

    @ApiOperation("查询匹配流水信息详情-流程使用")
    @AnonymousAccess(value = "/getApplyDetail", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "查询匹配流水信息详情-流程使用", type = LogActionType.SELECT, menuName = LogMenuEnum.VMS_MATCH_FLOW)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyId", value = "审批流id", dataTypeClass = Long.class, dataType = "Long", required = true)
    })
    public Result<VmsMatchInvoiceInfoVo> getApplyDetail(@RequestParam(value = "applyId") Long applyId) {
        log.info("{}查询匹配流水信息详情-流程使用，审批流id：{}", LOG_MSG, applyId);
        return vmsMatchFlowService.getApplyDetail(applyId);
    }

    @ApiOperation("查询匹配流水信息详情-页面使用")
    @AnonymousAccess(value = "/getMatchDetail", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "查询匹配流水信息详情-页面使用", type = LogActionType.SELECT, menuName = LogMenuEnum.VMS_MATCH_FLOW)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "matid", value = "匹配流水记录id", dataTypeClass = Long.class, dataType = "Long", required = true)
    })
    public Result<VmsMatchRecordVo> getMatchDetail(@RequestParam(value = "matid") Long matid) {
        log.info("{}查询匹配流水信息详情-页面使用，匹配流水记录id：{}", LOG_MSG, matid);
        return vmsMatchFlowService.getMatchDetail(matid);
    }

    @ApiOperation("根据发票明细查询已匹配的交易流水-分页查询")
    @AnonymousAccess(value = "/queryFlowByItem", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "根据发票明细查询已匹配的交易流水-分页查询", type = LogActionType.SELECT, menuName = LogMenuEnum.VMS_MATCH_FLOW)
    public Result<IPage<VmsTransactionFlowInvoicePageVo>> queryFlowByItem(@RequestBody @Validated VmsItemFlowPageDto pageDto) {
        pageDto.setDeptCode(getDeptCode());
        log.info("{}根据发票明细查询已匹配的交易流水，入参：{}", LOG_MSG, JSONObject.toJSONString(pageDto));
        return vmsMatchFlowService.queryFlowByItem(pageDto);
    }

    @ApiOperation("匹配流水或手工匹配-分页查询可匹配流水")
    @AnonymousAccess(value = "/queryFlowByAutoOrHand", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "匹配流水/手工匹配-分页查询可匹配流水", type = LogActionType.SELECT, menuName = LogMenuEnum.VMS_MATCH_FLOW)
    public Result<IPage<VmsTransactionFlowInvoicePageVo>> queryFlowByAutoOrHand(@RequestBody @Validated VmsQueryMatchFlowPageDto pageDto) {
        pageDto.setDeptCode(getDeptCode());
        log.info("{}匹配流水/手工匹配-分页查询可匹配流水，入参：{}", LOG_MSG, JSONObject.toJSONString(pageDto));
        return vmsMatchFlowService.queryFlowByAutoOrHand(pageDto);
    }

    @ApiOperation("匹配流水或手工匹配-匹配流水")
    @AnonymousAccess(value = "/itemMatchFlow", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "匹配流水/手工匹配-匹配流水", type = LogActionType.ADD, menuName = LogMenuEnum.VMS_MATCH_FLOW)
    public Result<Object> itemMatchFlow(@RequestBody @Validated VmsItemMatchFlowDto dto) {
        log.info("{}匹配流水/手工匹配-匹配流水，入参：{}", LOG_MSG, JSONObject.toJSONString(dto));
        return vmsMatchFlowService.itemMatchFlow(dto, getUser());
    }

    @ApiOperation("手工匹配-导入查询")
    @AnonymousAccess(value = "/readExcel", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "手工匹配-导入查询", type = LogActionType.UPLOAD, menuName = LogMenuEnum.VMS_MATCH_FLOW)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "excel文件", dataTypeClass = MultipartFile.class, dataType = "MultipartFile", required = true)
    })
    public Result<List<VmsTransactionFlowInvoicePageVo>> readExcel(@RequestParam(value = "file") MultipartFile file) {
        log.info("{}手工匹配-导入查询", LOG_MSG);
        return vmsMatchFlowService.readExcel(file);
    }

    @ApiOperation("匹配流水-审批人员保存允许差异额")
    @AnonymousAccess(value = "/saveAllowError", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "匹配流水-审批人员保存允许差异额", type = LogActionType.ADD, menuName = LogMenuEnum.VMS_MATCH_FLOW)
    public Result<Object> saveAllowError(@RequestBody @Validated VmsMatchAllowErrorDto dto) {
        log.info("{}匹配流水-审批人员保存允许差异额，入参：{}", LOG_MSG, JSONObject.toJSONString(dto));
        return vmsMatchFlowService.saveAllowError(dto);
    }

}

